package cn.example.providertest;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private String newId;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button addData = findViewById(R.id.add_data);
        Button queryData = findViewById(R.id.query_data);
        Button updateData = findViewById(R.id.update_data);
        Button deleteData = findViewById(R.id.delete_data);

        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://cn.example.databasetest.provider/book");
                ContentValues values = new ContentValues();
                values.put("name", "A Clash of Kings");
                values.put("author", "George Martin");
                values.put("pages", 1040);
                values.put("price", 22.85);
                getContentResolver().insert(uri, values);
            }
        });
        queryData.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://cn.example.databasetest.provider/book");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                if(cursor.moveToFirst()){
                    Log.d(TAG, "The book name is " + cursor.getString(cursor.getColumnIndex("name")));
                    Log.d(TAG, "The book author is " + cursor.getString(cursor.getColumnIndex("author")));
                    Log.d(TAG, "The book pages is " + cursor.getInt(cursor.getColumnIndex("pages")));
                    Log.d(TAG, "The book price is " + cursor.getDouble(cursor.getColumnIndex("price")));
                }
            }
        });
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://cn.example.databasetest.provider/book");
                ContentValues values = new ContentValues();
                values.put("pages", 1510);
                values.put("price", 24.79);
                getContentResolver().update(uri, values, "name = ?", new String[]{"A Clash of Kings"});
            }
        });
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://cn.example.databasetest.provider/book");
                getContentResolver().delete(uri, "name = ?", new String[]{"A Clash of Kings"});
            }
        });
    }
}